<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="姓名" prop="name">
        <el-select v-model="queryParams.name" placeholder="请选择人员姓名" clearable size="small">
          <el-option
            v-for="dict in filteredUsers"
            :key="dict.userId"
            :label="dict.nickName "
            :value="dict.userId"
          />
        </el-select>
<!--        <el-input-->
<!--          v-model="queryParams.name"-->
<!--          placeholder="请输入姓名"-->
<!--          clearable-->
<!--          size="small"-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
      </el-form-item>
<!--      <el-form-item label="岗位" prop="post">-->
<!--        <el-input-->
<!--          v-model="queryParams.post"-->
<!--          placeholder="请输入岗位"-->
<!--          clearable-->
<!--          size="small"-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="年龄" prop="age">-->
<!--        <el-input-->
<!--          v-model="queryParams.age"-->
<!--          placeholder="请输入年龄"-->
<!--          clearable-->
<!--          size="small"-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="工龄" prop="workingAge">-->
<!--        <el-input-->
<!--          v-model="queryParams.workingAge"-->
<!--          placeholder="请输入工龄"-->
<!--          clearable-->
<!--          size="small"-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="联系地址" prop="contactAddress">-->
<!--        <el-input-->
<!--          v-model="queryParams.contactAddress"-->
<!--          placeholder="请输入联系地址"-->
<!--          clearable-->
<!--          size="small"-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="参加工作时间" prop="workTime">-->
<!--        <el-date-picker clearable size="small"-->
<!--          v-model="queryParams.workTime"-->
<!--          type="date"-->
<!--          value-format="yyyy-MM-dd"-->
<!--          placeholder="选择参加工作时间">-->
<!--        </el-date-picker>-->
<!--      </el-form-item>-->
<!--      <el-form-item label="上岗证(职鉴)" prop="workPermit">-->
<!--        <el-input-->
<!--          v-model="queryParams.workPermit"-->
<!--          placeholder="请输入上岗证(职鉴)"-->
<!--          clearable-->
<!--          size="small"-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="文化程度" prop="educationalLevel">-->
<!--        <el-input-->
<!--          v-model="queryParams.educationalLevel"-->
<!--          placeholder="请输入文化程度"-->
<!--          clearable-->
<!--          size="small"-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="毕业院校" prop="graduatedFrom">-->
<!--        <el-input-->
<!--          v-model="queryParams.graduatedFrom"-->
<!--          placeholder="请输入毕业院校"-->
<!--          clearable-->
<!--          size="small"-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="所学专业" prop="major">-->
<!--        <el-input-->
<!--          v-model="queryParams.major"-->
<!--          placeholder="请输入所学专业"-->
<!--          clearable-->
<!--          size="small"-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="毕业时间" prop="graduationTime">-->
<!--        <el-date-picker clearable size="small"-->
<!--          v-model="queryParams.graduationTime"-->
<!--          type="date"-->
<!--          value-format="yyyy-MM-dd"-->
<!--          placeholder="选择毕业时间">-->
<!--        </el-date-picker>-->
<!--      </el-form-item>-->
<!--      <el-form-item label="备用" prop="remarkSpare">-->
<!--        <el-input-->
<!--          v-model="queryParams.remarkSpare"-->
<!--          placeholder="请输入备用"-->
<!--          clearable-->
<!--          size="small"-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="备用1" prop="remarkSpare1">-->
<!--        <el-input-->
<!--          v-model="queryParams.remarkSpare1"-->
<!--          placeholder="请输入备用1"-->
<!--          clearable-->
<!--          size="small"-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="备用2" prop="remarkSpare2">-->
<!--        <el-input-->
<!--          v-model="queryParams.remarkSpare2"-->
<!--          placeholder="请输入备用2"-->
<!--          clearable-->
<!--          size="small"-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['ledger:situation:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['ledger:situation:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['ledger:situation:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
		  :loading="exportLoading"
          @click="handleExport"
          v-hasPermi="['ledger:situation:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="situationList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="序号" type="index" width="50" align="center">
        <template slot-scope="scope">
          <span>{{(queryParams.pageNum - 1) * queryParams.pageSize + scope.$index + 1}}</span>
        </template>
      </el-table-column>
<!--      <el-table-column label="主键ID" align="center" prop="id" />-->
      <el-table-column label="姓名" align="center" prop="name" />
      <el-table-column label="岗位" align="center" prop="post" />
      <el-table-column label="年龄" align="center" prop="age" />
      <el-table-column label="工龄" align="center" prop="workingAge" />
      <el-table-column label="联系地址" align="center" prop="contactAddress" />
      <el-table-column label="参加工作时间" align="center" prop="workTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.workTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="上岗证(职鉴)" align="center" prop="workPermit" />
      <el-table-column label="文化程度" align="center" prop="educationalLevel" />
      <el-table-column label="毕业院校" align="center" prop="graduatedFrom" />
      <el-table-column label="所学专业" align="center" prop="major" />
      <el-table-column label="毕业时间" align="center" prop="graduationTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.graduationTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
<!--      <el-table-column label="备注" align="center" prop="remark" />-->
<!--      <el-table-column label="备用" align="center" prop="remarkSpare" />-->
<!--      <el-table-column label="备用1" align="center" prop="remarkSpare1" />-->
<!--      <el-table-column label="备用2" align="center" prop="remarkSpare2" />-->
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['ledger:situation:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['ledger:situation:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改人员情况汇总对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="800px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <el-row :gutter="20">
          <el-col :span="12" :xs="24">
        <el-form-item label="姓名" prop="name">
          <el-select  v-model="form.name" filterable placeholder="选择人员" @change="handleChange"  style="width: 100%">
            <el-option
              v-for="user in filteredUsers"
              :key="user.userId+''"
              :label="user.nickName"
              :value="user.userId+''">
            </el-option>
          </el-select>
<!--          <el-input v-model="form.name" placeholder="请输入姓名" />-->
        </el-form-item>
          </el-col>
          <el-col :span="12" :xs="24">
        <el-form-item label="岗位" prop="post">
          <el-select v-model="form.post" placeholder="请选择岗位"  style="width: 100%">
            <el-option
              v-for="item in postOptions"
              :key="item.postId"
              :label="item.postName"
              :value="item.postId"
              :disabled="item.status == 1"
            ></el-option>
          </el-select>
<!--          <el-input v-model="form.post" placeholder="请输入岗位" />-->
        </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="12" :xs="24">
        <el-form-item label="年龄" prop="age">
          <el-input-number v-model="form.age" placeholder="请输入年龄"  style="width: 100%"/>
        </el-form-item>
          </el-col>
          <el-col :span="12" :xs="24">
        <el-form-item label="工龄" prop="workingAge">
          <el-input-number v-model="form.workingAge" placeholder="请输入工龄"  style="width: 100%"/>
        </el-form-item>
          </el-col>
        </el-row>
        <el-form-item label="联系地址" prop="contactAddress">
          <el-input v-model="form.contactAddress" placeholder="请输入联系地址" />
        </el-form-item>
        <el-form-item label="上岗证(职鉴)" prop="workPermit">
          <el-input v-model="form.workPermit" placeholder="请输入上岗证(职鉴)" />
        </el-form-item>
        <el-row :gutter="20">
          <el-col :span="12" :xs="24">
        <el-form-item label="参加工作时间" prop="workTime">
          <el-date-picker clearable size="small"
                          v-model="form.workTime"
                          type="date"
                          value-format="yyyy-MM-dd"
                          placeholder="选择参加工作时间"  style="width: 100%">
          </el-date-picker>
        </el-form-item>
          </el-col>
          <el-col :span="12" :xs="24">
        <el-form-item label="文化程度" prop="educationalLevel">
          <el-input v-model="form.educationalLevel" placeholder="请输入文化程度" />
        </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="12" :xs="24">
        <el-form-item label="毕业院校" prop="graduatedFrom">
          <el-input v-model="form.graduatedFrom" placeholder="请输入毕业院校" />
        </el-form-item>
          </el-col>
          <el-col :span="12" :xs="24">
            <el-form-item label="毕业时间" prop="graduationTime">
              <el-date-picker clearable size="small"
                              v-model="form.graduationTime"
                              type="date"
                              value-format="yyyy-MM-dd"
                              placeholder="选择毕业时间"  style="width: 100%">
              </el-date-picker>
            </el-form-item>
          </el-col>
        </el-row>
        <el-form-item label="所学专业" prop="major">
          <el-input v-model="form.major" placeholder="请输入所学专业" />
        </el-form-item>

<!--        <el-form-item label="备注" prop="remark">-->
<!--          <el-input v-model="form.remark" placeholder="请输入备注" />-->
<!--        </el-form-item>-->
<!--        <el-form-item label="备用" prop="remarkSpare">-->
<!--          <el-input v-model="form.remarkSpare" placeholder="请输入备用" />-->
<!--        </el-form-item>-->
<!--        <el-form-item label="备用1" prop="remarkSpare1">-->
<!--          <el-input v-model="form.remarkSpare1" placeholder="请输入备用1" />-->
<!--        </el-form-item>-->
<!--        <el-form-item label="备用2" prop="remarkSpare2">-->
<!--          <el-input v-model="form.remarkSpare2" placeholder="请输入备用2" />-->
<!--        </el-form-item>-->
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listSituation, getSituation, delSituation, addSituation, updateSituation, exportSituation, optionselect } from "@/api/ledger/situation";
import { listUserNoPage } from '@/api/system/user'

export default {
  name: "Situation",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 人员情况汇总表格数据
      situationList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 岗位选项
      postOptions: [],
      filteredUsers:[],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        name: null,
        post: null,
        age: null,
        workingAge: null,
        contactAddress: null,
        workTime: null,
        workPermit: null,
        educationalLevel: null,
        graduatedFrom: null,
        major: null,
        graduationTime: null,
        remarkSpare: null,
        remarkSpare1: null,
        remarkSpare2: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        name: [
          { required: true, message: "名称不能为空", trigger: "blur" }
        ],
        post: [
          { required: true, message: "岗位不能为空", trigger: "blur" }
        ],
        age: [
          { required: true, message: "年龄不能为空", trigger: "blur" }
        ],
        workingAge: [
          { required: true, message: "工龄不能为空", trigger: "blur" }
        ],
        workTime: [
          { required: true, message: "参加工作时间不能为空", trigger: "blur" }
        ],
        workPermit: [
          { required: true, message: "上岗证(职鉴)不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.fetchUsers();
    this.getList();
    /** 查询字典下拉列表 */
    optionselect().then(response => {
      this.postOptions = response.data;
    });
  },
  methods: {
    /** 查询人员情况汇总列表 */
    getList() {
      this.loading = true;
      listSituation(this.queryParams).then(response => {
        this.situationList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    fetchUsers() {
      this.loading=true;
      listUserNoPage().then(response => {
          this.filteredUsers = response.rows;
          this.loading = false;
        }
      );
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        name: null,
        post: null,
        age: null,
        workingAge: null,
        contactAddress: null,
        workTime: null,
        workPermit: null,
        educationalLevel: null,
        graduatedFrom: null,
        major: null,
        graduationTime: null,
        remark: null,
        remarkSpare: null,
        remarkSpare1: null,
        remarkSpare2: null,
        createBy: null,
        createTime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加人员情况汇总";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getSituation(id).then(response => {
        this.form = response.data;
        this.form.post = parseInt(response.data.post, 10);
        this.open = true;
        this.title = "修改人员情况汇总";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateSituation(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addSituation(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm('是否确认删除人员情况汇总编号为"' + ids + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delSituation(ids);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有人员情况汇总数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(() => {
          this.exportLoading = true;
          // return exportSituation(queryParams);
        this.download('/ledger/situation/export', {
          ...this.queryParams
        }, `人员情况汇总.xlsx`)
        this.exportLoading = false;
        }).then(response => {
          // this.download(response.msg);
          // this.exportLoading = false;
        }).catch(() => {});
    },
    handleChange(value) {
      console.log("Selected user ID:", value)
    },
  }
};
</script>
